<?php
// 本类由系统自动生成，仅供测试用途
class CustomerAction extends CommonAction {
	public function _filter(&$map){
		$map['removed']=1;
		$map['id'] = array('not in ','1,2,3');
	}
	 public function _before_insert() {
		$model = M ( "Customer" );
		if ($model->where ( "customername='" . $_POST ['customername']."'" )->select ()) { // 判断客户名是否已存在
			$this->error ( '客户名称重复!');
			exit ();
		} 
	}
	public function _before_update() {
		$model = M ( "Customer" );
		$customername = $model->find ( $_POST ['id'] );
		if ($customername ['customername'] != $_POST ['customername']) {
			if ($model->where ( array (
					'customername' => $_POST ['customername'] 
			) )->select ()) {
				$this->error ( '客户名称重复!' );
				exit ();
			}
		}
	} 
	public function _before_edit() {
		$model = M ( "Customer" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	public function read(){
	    $model = M('Customer');
	    $id=$_REQUEST['id'];
	    $vo=$model->where('id='.$id)->find();
	    $this->assign('vo',$vo);
	    $this->display();
	}
	/**
	 * 查看供货信息
	 *
	 */
	public function readSupply() {
		$model = M ( "Customer" );
		$id=$_REQUEST['id'];
		$vo = $model->where('id='.$id)->find();
		$this->assign('vo',$vo);
		$this->display();
	}
	
	public function searchSupply() {
		$model = M ( 'CustomerSupply' );
		if (isset ( $_POST['id'] )) {
			$list = $model->where ( 'id=' .$_POST['id'] )->select ();
			$this->assign ( 'vo', $list );
		}
		$this->display ( 'readSupply' );
	}
	public function getcontract(){
		$customerid = $_SESSION ['id'];
		$model = M ( 'Customer' );
		$name = $model->query ( 'select customername from mlt_customer where mlt_customer.id=' . $customerid );
		$name1=$name[0]['customername'];
		$contract=M('Contract');
		$list1=$contract->where("type=1 and secondname='".$name1."'")->select();
		$this->assign ( 'list1', $list1 );
		$this->display();
	}
	public function payAdd() {
		$customerid = $_REQUEST ['id'];
		$_SESSION['id']	=$customerid;
		$this->assign ( 'customerid', $customerid );
		$model = M ( 'Customer' );
		$name = $model->query ( 'select customername from mlt_customer where mlt_customer.id=' . $customerid );
		$this->assign ( 'customername', $name [0] ['customername'] );
		$this->display();
		
	}
	/**
	 * 插入付款记录
	 */
	public function insertPay() {
		$model = M ( 'CustomerPayment' );
		$contractno=$_POST['contractno'];
		$paymuch=$_POST['paymuch'];
		$contract=M('Contract');
		$contractlist=$contract->where("contractno='".$contractno."'")->find();
		if($contractlist['wpayment']==null){
			if($paymuch>$contractlist['totalprice']){
				$this->ajaxReturn(1,'付款大于总额，请重新输入',300);
			}else{
				$data['apayment']=$contractlist['apayment']+$paymuch;
				$data['wpayment']=$contractlist['totalprice']-$contractlist['apayment']-$paymuch;
				$contract->where("contractno='".$contractno."'")->save($data);
				$_POST['haspay']=$contractlist['apayment']+$paymuch;
				$_POST['nopay']=$contractlist['totalprice']-$contractlist['apayment']-$paymuch;
			}			
		}else{
			if($contractlist['wpayment']!=0){
				if($paymuch>$contractlist['wpayment']){
					$this->ajaxReturn(1,'付款大于总额，请重新输入',300);
				}else{
					$data['apayment']=$contractlist['apayment']+$paymuch;
					$data['wpayment']=$contractlist['totalprice']-$contractlist['apayment']-$paymuch;
					$contract->where("contractno='".$contractno."'")->save($data);
					$_POST['haspay']=$contractlist['apayment']+$paymuch;
					$_POST['nopay']=$contractlist['totalprice']-$contractlist['apayment']-$paymuch;
				}				
			}else{
				$this->ajaxReturn(1,'款已付清！',300);
			}
		}	
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 保存当前数据对象
		$list = $model->add ();
		if ($list !== false) { // 保存成功
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->ajaxReturn ( 1, '付款录入成功!', 200 );
		} else {
			// 失败提示
			$this->ajaxReturn ( 1, '付款录入失败!', 300 );
		}
	}
	/**
	 * 读取付款记录
	 */
	public function readPay() {
		$custmoerid=$_REQUEST['id'];
		$model=M('CustomerPayment');
		$name=$model->query('select customername from mlt_customer where mlt_customer.id='.$custmoerid);
		import ( '@.Org.Page' );
		$count = $model->where('customerid='.$custmoerid)->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where('customerid='.$custmoerid)->page ( $pageNum . "," . $page->listRows )->select ();
		
		$this->assign('customername',$name[0]['customername']);
		$this->assign ( 'id', $custmoerid);
		$this->assign('list',$list);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	//双击查看付款单条记录
	public function viewaPay() {
		$id=$_REQUEST['id'];
		$model=M('CustomerPayment');		
		$vo = $model->where('id='.$id)->find ();
		$name=$model->query('select customername from mlt_customer where mlt_customer.id='.$vo['customerid']);
		$this->assign('customername',$name[0]['customername']);
		$this->assign('vo',$vo);
		$this->display();
	}
	public function supplyEdit() {
		$id=$_REQUEST['id'];
		$Freight=M('Customer');
		$result=$Freight->where('id='.$id)->find();
		$this->assign('vo',$result);
		$this->display();
	}
	public function supplyUpdate() {
	$id=$_REQUEST['id'];
		$Freight=M('Customer');
		if($Freight->create())
		{
			$result=$Freight->where('id='.$id)->save();
			if($result)
			{
				$this->ajaxReturn($result,'更新成功',200);
			}
			else
			{
				$this->ajaxReturn($result,'更新失败',300);
			}
		}
		else
		{
			$this->error($dispatch->getError());
		}
}

//导出功能
public function export(){

	$model=D('Customer');
	 $customername=$_REQUEST['customername'];
	 $customertype=$_REQUEST['customertype'];
	// $sql="select * from mlt_customer where 1=1";
	if(!empty($customername)){
		$map['customername']=array('like','%'.$customername.'%');
		//$sql.=" and customername like '%" .$customername."%'";
	}
	 if(!empty($customertype)){
		$map['customertype']=array('like','%'.$customertype.'%');
		//$sql.=" and customertype like '%" .$customertype."%'";
	} 
 	$Customer=$model->where($map)->field('')->select();
 	//$Customer=$model->query($sql);

 	$cname=date('Y-m-d');
		
 	$map = array('客户号','客户类型','客户名称','业务员','电话','传真','网址','邮箱','城市','邮编','地址','开户银行','银行账号','税号','备注');
 	$field=array('customerno','customertype','customername','customermanager','phone','fax','site','email','city','zip','address','openbank','accountsno','taxno','notes');		
	$this->phpexcel($Customer, $cname, $map,$field);
		
}


}